package com.liwenfeng.file;

import java.io.File;
import java.io.IOException;

/*
 * 递归方法查找文件
 * */
public class SearchFile {
    public static void main(String[] args) throws IOException {
        new SearchFile().serch(new File("D:/code"), "GameDemo");
    }

    public void serch(File dir, String filename) throws IOException {
        if (dir == null || !dir.exists() || dir.isFile()) throw new IllegalArgumentException("目录不存在或是一个文件！");
        File[] files = dir.listFiles();
        if (files != null && files.length > 0) {
            for (File file : files) {
                if (file.isFile()) {
                    if (file.getName().contains(filename)) {
                        System.out.println("找到文件：" + file.getAbsolutePath());
                        //找到文件后立即启动此程序
//                        Runtime runtime = Runtime.getRuntime();
//                        runtime.exec(new String[]{file.getAbsolutePath()});
                    }
                } else {
                    serch(file, filename);
                }
            }
        }
    }
}
